<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    // 前序遍历
    var preorderTraversal = (root,res = []) => {
      if(!root) return [];
      const stack = [];
      stack.push(root);
      while(stack.length) {
        const node = stack.pop();
        if(!node) {
          res.push(stack.pop().val);
          continue;
        }
        node.right && stack.push(node.right);
        node.left && stack.push(node.left);
        stack.push(node);
        stack.push(null);
      }
      return res;
    }
  </script>
</body>
</html>